﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using System.Linq.Expressions;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// A parameter responsible for creating a LambdaExpression that performs a range comparisson.
    /// </summary>
    public class RangeExpressionParameter : DynamicExpressionParameterBase
    {
        [DefaultValue("")]
        public string MinValue
        {
            get
            {
                object obj2 = this.ViewState["MinValue"];
                if (obj2 == null)
                {
                    return null;
                }
                return (string)obj2;
            }
            set
            {
                if (this.MinValue != value)
                {
                    this.ViewState["MinValue"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        [DefaultValue("")]
        public string MaxValue
        {
            get
            {
                object obj2 = this.ViewState["MaxValue"];
                if (obj2 == null)
                {
                    return null;
                }
                return (string)obj2;
            }
            set
            {
                if (this.MaxValue != value)
                {
                    this.ViewState["MaxValue"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        public override LambdaExpression GetLambdaExpression(Type itType)
        {
            string predicate;
            object[] values;

            if (!string.IsNullOrEmpty(MinValue) && !string.IsNullOrEmpty(MaxValue))
            {
                predicate = string.Format("{0} >= @0 AND {0} <= @1", this.Name);
                values = new object[] { MinParameterValue, MaxParameterValue };
            }
            else if (!string.IsNullOrEmpty(MinValue))
            {
                predicate = string.Format("{0} >= @0", this.Name);
                values = new object[] { MinParameterValue };
            }
            else if (!string.IsNullOrEmpty(MaxValue))
            {
                predicate = string.Format("{0} <= @0", this.Name);
                values = new object[] { MaxParameterValue };
            }
            else
                return null;

            return DynamicExpression.ParseLambda(itType, typeof(bool), predicate, values);
        }

        protected object MinParameterValue
        {
            get
            {
                return GetValue(MinValue, null, Type, this.ConvertEmptyStringToNull, false);
            }
        }

        protected object MaxParameterValue
        {
            get
            {
                return GetValue(MaxValue, null, Type, this.ConvertEmptyStringToNull, false);
            }
        }
    }
}
